﻿using System.Text;

namespace OConf.NetCore.Common
{
    public class FileCore
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static bool CreateDirectory(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            return true;
        }

        /// <summary>
        /// 读取指定文件里的信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath)
        {
            string fileContent = string.Empty;
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    fileContent = sr.ReadToEnd();
                }
            }
            return fileContent;
        }

        /// <summary>
        /// 将内容保存到指定文件中
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileContent"></param>
        /// <returns></returns>
        public static bool SaveFile(string filePath, string fileContent)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    sw.Write(fileContent);
                }
            }
            return true;
        }
    }
}
